//
//  CMLoginCtrl.m
//  CreditManger
//
//  Created by sky on 15/10/14.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMLoginCtrl.h"

#import "CMHomeCtrl.h"

#import "CMRegistCtrl.h"
#import "CMFPhoneNumCtrl.h"

#import "CMLoginParam.h"
#import "CMLoginParser.h"
#import "CMLoginUserInfo.h"
#import "CMLoginManager.h"
#import "CheckValid.h"
#import "CMBankPerfInformatCtrl.h"


@interface CMLoginCtrl ()<UITextFieldDelegate>
{
    UIScrollView *_bgview;
    UIImageView *_headview;
    UITextField *_username;
    UITextField *_password;
}

@end

@implementation CMLoginCtrl

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

-(void)buildLayout{
    
    _bgview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    UIImage *bgim = [UIImage imageNamed:@"loginBg"];
    UIImageView *bgvi = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    bgvi.image = bgim;
    [self.view addSubview:_bgview];
    [_bgview addSubview:bgvi];
    
    UIImage *head = [UIImage imageNamed:@"head_place"];
    _headview = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - head.size.width/2, 230/2, head.size.width, head.size.height)];
    _headview.layer.cornerRadius = head.size.width * 0.5;
    _headview.clipsToBounds = YES;
    _headview.image = head;
    _headview.backgroundColor = [UIColor whiteColor];
    [_bgview addSubview:_headview];
    
    _username = [self TextFieldWithRect:CGRectMake(50, _headview.origin.y + _headview.size.height + 170/2, SCREEN_WIDTH - 100, 18) leftImage:[UIImage imageNamed:@"login_username"] placeHold:@"请输入账号" index:1];
    _username.clearButtonMode = YES;
    [_bgview addSubview:_username];
    
    _password = [self TextFieldWithRect:CGRectMake(50, _username.origin.y + 18 + 15 + 15, SCREEN_WIDTH - 100 - 14 * 5 - 5, 18) leftImage:[UIImage imageNamed:@"login_password"] placeHold:@"请输入密码" index:2];
    _password.secureTextEntry = YES;
    _password.clearButtonMode = YES;
    [_bgview addSubview:_password];
    
    UILabel *forgetlbl = [self LabelWithRect:CGRectMake(SCREEN_WIDTH - 14 * 5 - 50 , _password.origin.y , 14 * 5, 14) textColor:[LCCommon getColor:@"2999f9"] textstr:@"忘记密码?" textfont:14 textstyle:NSTextAlignmentRight];
    forgetlbl.userInteractionEnabled = YES;
    [_bgview addSubview:forgetlbl];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forgetpress)];
    [forgetlbl addGestureRecognizer:tap];
    
    UIButton *loginbtn = [[UIButton alloc]initWithFrame:CGRectMake(50, forgetlbl.origin.y + 18 + 15 + 15, SCREEN_WIDTH - 100, 45)];
    loginbtn.backgroundColor = [LCCommon getColor:@"f7514a"];
    [loginbtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginbtn setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.2] forState:UIControlStateNormal];
    loginbtn.titleLabel.font = [UIFont systemFontOfSize:15];
    loginbtn.layer.cornerRadius = 2.5;
    [loginbtn addTarget:self action:@selector(loginpress) forControlEvents:UIControlEventTouchUpInside];
    [_bgview addSubview:loginbtn];
    
    UILabel *regist = [self LabelWithRect:CGRectMake(SCREEN_WIDTH/2 - 63, loginbtn.frame.origin.y + 45 + 20, 14 * 9, 14) textColor:[LCCommon getColor:@"666666"] textstr:@"还没账号?立即注册" textfont:14 textstyle:NSTextAlignmentCenter];
    regist.userInteractionEnabled = YES;
    [_bgview addSubview:regist];
    
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:regist.text];
    [noteStr addAttribute:NSForegroundColorAttributeName value:[LCCommon getColor:@"33CC99"] range:NSMakeRange(5, 4)];
    [regist setAttributedText:noteStr] ;
  
    
    UITapGestureRecognizer *res = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(registpress)];
    [regist addGestureRecognizer:res];
    
    [self addTap];
}



-(void)doTouch:(UIGestureRecognizer *)sender
{
    [self.view endEditing:YES];
    LLog(@"");
}



#pragma mark - login 

-(void)loginpress{
    LLog(@"login");
    
    if (![CheckValid isValidatePhone:_username.text])
    {
        [_username becomeFirstResponder];
        DISSMISS_ERRView(@"用户名不正确",[[[UIApplication sharedApplication] windows] lastObject]);
    }
    else if([CheckValid checkStrIsEmpty:_password.text])
    {
        [_password becomeFirstResponder];
        DISSMISS_ERRView(@"用户密码不能为空",[[[UIApplication sharedApplication] windows] lastObject]);
    }
    else
    {
        [_username resignFirstResponder];
        [_password resignFirstResponder];
        [self requestToLogin];
    }
//    CMBankPerfInformatCtrl *home = [[CMBankPerfInformatCtrl alloc]init];
//    [self.navigationController pushViewController:home animated:YES];
}


#pragma mark - forget

-(void)forgetpress{
    LLog(@"forget");
    CMFPhoneNumCtrl* getBackVC = [[CMFPhoneNumCtrl alloc] init];
    [self.navigationController pushViewController:getBackVC animated:YES];
}

#pragma mark - regist

-(void)registpress{
     LLog(@"registpress");
    CMRegistCtrl* registVC = [[CMRegistCtrl alloc] init];
    [self.navigationController pushViewController:registVC animated:YES];
   
}


#pragma mark - MakeText

-(UITextField *)TextFieldWithRect:(CGRect)rect leftImage:(UIImage *)image placeHold:(NSString *)placeHold index:(NSInteger)index
{
    UITextField *text = [[UITextField alloc] initWithFrame:rect];
    text.placeholder = placeHold;
    text.backgroundColor = [UIColor clearColor];
//    text.backgroundColor = [UIColor blackColor];
    text.delegate = self;
    text.clearButtonMode = YES;
    text.textColor = [LCCommon getColor:@"333333"];
    text.font = [UIFont systemFontOfSize:14];
    [text setFont:[UIFont systemFontOfSize:14]];
    [text setValue:[LCCommon getColor:@"cccccc"] forKeyPath:@"_placeholderLabel.textColor"];
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width + 20/2, rect.size.height)];
    view.backgroundColor = [UIColor clearColor];
    CGFloat width = image.size.width;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (view.height - width)/2, width, width)];
    
    if (index == 0) {
        imageView.frame = CGRectMake(0, view.height - 10 - width, width, width);
    }
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(text.origin.x, text.origin.y + text.size.height + 15, SCREEN_WIDTH - 100, .5)];
    line.backgroundColor = [LCCommon getColor:@"cccccc"];
    [_bgview addSubview:line];
    
    imageView.image = image;
    [view addSubview:imageView];
    text.leftView = view;
    text.leftViewMode = UITextFieldViewModeAlways;
    view = nil;
    imageView = nil;
    
    return text;
}



-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    label.textColor = color;
    label.text = str;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    return label;
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - net method

- (void)requestToLogin
{
    CMLoginParam *param = [[CMLoginParam alloc]init];
    param.mobile = _username.text;
    param.password = _password.text;

    
    CMLoginParser *parser = [[CMLoginParser alloc]init];
    [NetCenter postDataFromURL:Method_login params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code == 0)
        {
            CMLoginUserInfo *info = (CMLoginUserInfo *)myResult.result;
            [CMLoginManager cacheCurrentUserToUserDefault:info];
            
            CMHomeCtrl *login = [[CMHomeCtrl alloc]init];
            CMNavigationController *nav = [[CMNavigationController alloc]initWithRootViewController:login];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
        
    } failure:^(NSError *error) {
        DISSMISS_ERR([error description]);
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
